<?php
/**
 * Created by PhpStorm.
 * User: YFTD
 * Date: 2019/3/18
 * Time: 16:12
 */

namespace App\Repositories\Shop;


use App\Models\Logistics\Logistics;
use App\Models\Logistics\LogisticsPrice;
use App\Repositories\Repository;
use Illuminate\Support\Facades\DB;

class LogisticsRepository extends Repository
{
    public function index()
    {
        $seller = $this->getUser();
        $logistics = Logistics::query()->where('shop_id', $seller->shop_id)
            ->paginate($this->page_size, ['*'], 'page', $this->page);
        return $this->respondWith($logistics);
    }


    public function show($logistics_id)
    {
        $seller = $this->getUser();
        $logistics = Logistics::query()->with('price')->where('shop_id', $seller->shop_id)
            ->where('logistics_id', $logistics_id)
            ->first();
        if ($logistics && $logistics->price) {
            $logistics->price = $this->showPrice($logistics->price);
        }
        return $this->respondWith($logistics);
    }


    public function store($data)
    {
        $seller = $this->getUser();
        $data['shop_id'] = $seller->shop_id;
        DB::beginTransaction();
        try {
            $logistic = Logistics::query()->create($data);
            if (isset($data['logistics_price'])) {
                $price_data = [];
                foreach ($data['logistics_price'] as $item) {
                    $temp_data = $this->setLogisticsPriceData($item['area_code'], $item['price'], $logistic->logistic_id);
                    $price_data = array_merge($price_data, $temp_data);
                }
                if (count($price_data) > 0) {
                    LogisticsPrice::query()->insert($price_data);
                }
            }
            DB::commit();
            return $this->respondSuccess();
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->respondError(10110);
        }
    }


    public function save($data)
    {
        DB::beginTransaction();
        try {
            $logistic = Logistics::query()->find($data['logistics_id']);
            $logistic->fill($data)->save();
            if (isset($data['logistics_price'])) {
                LogisticsPrice::query()->where('logistics_id', $data['logistics_id'])->delete();
                $price_data = [];
                foreach ($data['logistics_price'] as $item) {
                    $temp_data = $this->setLogisticsPriceData($item['area_code'], $item['price'], $logistic->logistic_id);
                    $price_data = array_merge($price_data, $temp_data);
                }
                if (count($price_data) > 0) {
                    LogisticsPrice::query()->insert($price_data);
                }
            }
            DB::commit();
            return $this->respondSuccess();
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->respondError(10108);
        }
    }

    public function delete($logistics_id)
    {
        DB::beginTransaction();
        try {
            Logistics::query()->where('logistics_id', $logistics_id)->delete();
            LogisticsPrice::query()->where('logistics_id', $logistics_id)->delete();
            DB::commit();
            return $this->respondSuccess();
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->respondError(10108);
        }
    }


    private function setLogisticsPriceData($area_code, $price, $logistics_id)
    {
        $data = [];
        $time = date('Y-m-d H:i:s');
        foreach ($area_code as $item) {
            $data[] = [
                'area_code' => $item,
                'price' => $price,
                'logistics_id' => $logistics_id,
                'created_at' => $time,
                'updated_at' => $time
            ];
        }
        return $data;
    }


    private function showPrice($prices)
    {
        $data=[];
        foreach ($prices as $price) {
            $data[$price->price][] = $price;
        }
    }
}